package io.mft.runtime.function.initor;

import java.util.ArrayList;
import java.util.List;

import io.mft.api.domain.Api;
import io.mft.api.domain.ApiFunctionType;
import io.mft.api.domain.ApiRegulation;
import io.mft.runtime.cache.ApiCache;
import io.mft.runtime.function.ApiExecutorObject;
import io.mft.runtime.function.ApiFunctionInitor;
import io.mft.runtime.function.FunctionExecutor;
import io.mft.runtime.function.process.ApiFunctionExecutor;
import io.mft.runtime.function.process.executor.DataAccessProcessExecutor;
import io.mft.runtime.function.process.executor.InputParameterProcessExecutor;
import io.mft.runtime.function.process.executor.LogicProcessExecutor;
import io.mft.runtime.function.process.executor.OutputParameterProcessExecutor;
import io.mft.runtime.function.process.executor.cache.OutputCacheReadAccessProcessExecutor;
import io.mft.runtime.function.process.executor.cache.OutputCacheWriteAccessProcessExecutor;
/**
 * 原子API初始化器
 * @说明：TODO
 * @类型名称：ProcessExecutorApiFunctionInitor
 * @创建者: 胡常建  
 * @创建时间: 2021年2月21日 下午5:44:23
 * @修改者: 胡常建  
 * @修改时间: 2021年2月21日 下午5:44:23
 */
public class ApiFunctionExecutorInitor extends ApiFunctionInitor {

	public ApiFunctionExecutorInitor(ApiCache apiCache) {
		super(apiCache);
	}

	@Override
	protected ApiExecutorObject initApiExecutorObject(Api api) {
		ApiFunctionExecutor apiFunctionExecutor=new ApiFunctionExecutor();
		apiFunctionExecutor.setKey(api.getKey());
		List<FunctionExecutor> regulationExecutorList=new ArrayList<>();
		api.getApiRegulations().forEach(r->{
			FunctionExecutor regulationExecutor=instanceApiFunctionProcessRegulationExecutor(api.getKey(), r);
			regulationExecutorList.add(regulationExecutor);
		});
		FunctionExecutor[] regulationExecutors=new FunctionExecutor[regulationExecutorList.size()];
		regulationExecutorList.toArray(regulationExecutors);
		ApiExecutorObject apiExecutorObject=new ApiExecutorObject(api, apiFunctionExecutor,regulationExecutors);

		return apiExecutorObject;
	}
	
	private FunctionExecutor instanceApiFunctionProcessRegulationExecutor(String apiKey,ApiRegulation regulation) {
		FunctionExecutor functionExecutor=null;
		switch (regulation.getRegulation().getProcessClassify()) {
		case DataAccessProcessExecutor:
			DataAccessProcessExecutor dataAccessProcessExecutor=new DataAccessProcessExecutor();
			dataAccessProcessExecutor.setRegulation(regulation.getRegulation());
			String dataKey=apiKey+"."+"0."+regulation.getPos()+"."+DataAccessProcessExecutor.class.getSimpleName();
			dataAccessProcessExecutor.setKey(dataKey);
			functionExecutor= dataAccessProcessExecutor;
			break;
		case InputParameterProcessExecutor:
			InputParameterProcessExecutor inputParameterProcessExecutor=new InputParameterProcessExecutor();
			inputParameterProcessExecutor.setRegulation(regulation.getRegulation());
			String inputKey=apiKey+"."+"0."+regulation.getPos()+"."+InputParameterProcessExecutor.class.getSimpleName();
			inputParameterProcessExecutor.setKey(inputKey);
			functionExecutor=inputParameterProcessExecutor;
			break;
		case LogicProcessExecutor:
			LogicProcessExecutor logicProcessExecutor=new LogicProcessExecutor();
			logicProcessExecutor.setRegulation(regulation.getRegulation());
			String logicKey=apiKey+"."+"0."+regulation.getPos()+"."+LogicProcessExecutor.class.getSimpleName();
			logicProcessExecutor.setKey(logicKey);
			functionExecutor=logicProcessExecutor;
			break;
		case OutputParameterProcessExecutor:
			OutputParameterProcessExecutor outputParameterProcessExecutor=new OutputParameterProcessExecutor();
			outputParameterProcessExecutor.setRegulation(regulation.getRegulation());
			String outputKey=apiKey+"."+"0."+regulation.getPos()+"."+OutputParameterProcessExecutor.class.getSimpleName();
			outputParameterProcessExecutor.setKey(outputKey);
			functionExecutor=outputParameterProcessExecutor;
			break;
		case OutputCacheReadAccessProcessExecutor:
			OutputCacheReadAccessProcessExecutor outputCacheAccessProcessExecutor=new OutputCacheReadAccessProcessExecutor();
			outputCacheAccessProcessExecutor.setRegulation(regulation.getRegulation());
			String readKey=apiKey+"."+"0."+regulation.getPos()+"."+OutputCacheReadAccessProcessExecutor.class.getSimpleName();
			outputCacheAccessProcessExecutor.setKey(readKey);
			functionExecutor=outputCacheAccessProcessExecutor;
			break;
		case OutputCacheWriteAccessProcessExecutor:
			OutputCacheWriteAccessProcessExecutor outputCacheWriteAccessProcessExecutor=new OutputCacheWriteAccessProcessExecutor();
			outputCacheWriteAccessProcessExecutor.setRegulation(regulation.getRegulation());
			String writeKey=apiKey+"."+"0."+regulation.getPos()+"."+OutputCacheWriteAccessProcessExecutor.class.getSimpleName();
			outputCacheWriteAccessProcessExecutor.setKey(writeKey);
			functionExecutor=outputCacheWriteAccessProcessExecutor;
			break;
		default:
			break;
		}
		return functionExecutor;
	}

	@Override
	protected ApiFunctionType apiFunctionType() {
		return ApiFunctionType.processExecutor;
	}
}
